package org.huang.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author hdu_huang
 * @since 2024/1/19 11:03
 * 平台相关代码
 */

@Slf4j
public final class Platform {

    private Platform() {
    }

    private static final String OS_NAME = System.getProperty("os.name", "").toLowerCase(Locale.US);


    //noUse see PlatformDependent 复杂 amd64 x84 arm32 aarch64...
    private static final String OS_ARCH = System.getProperty("os.arch");

    //lineSeparator   win "\r\n"  linux/mac "\n"
    //path.separator  win ";"    linux/mac ":"
    //file.separator  win "\"    linux/mac "/"
    private static final String LINE_SEPARATOR = System.lineSeparator();

    private static final boolean IS_WINDOWS;
    private static final boolean IS_MACOS;

    //临时Directory
    private static final String TEMP_DIR_PATH = System.getProperty("java.io.tmpdir", "/tmp");
    private static final File TEMP_DIR;

    static {

        boolean windows = OS_NAME.contains("win");
        if (windows) {
            log.debug("Current Platform: Windows");
        }

        IS_WINDOWS = windows;

        String name = OS_NAME.replaceAll("[^a-z0-9]+", "");
        boolean osx = name.startsWith("macosx") || name.startsWith("osx");

        if (osx) {
            log.debug("Current Platform: MacOS");
        }

        IS_MACOS = osx;

        log.info("TEMP_DIR_PATH : {}", TEMP_DIR_PATH);
        TEMP_DIR = new File(TEMP_DIR_PATH);
    }

    public static void main(String[] args) {

        Map<String, String> env = System.getenv();
        Properties properties = System.getProperties();


        System.out.println(OS_NAME);

    }

    public static <K, V> HashMap<K, V> newHashMap(Map<? extends K, ? extends V> map) {
        return new HashMap<K, V>(map);
    }
    public static <K, V> ConcurrentMap<K, V> newConcurrentHashMap() {
        return new ConcurrentHashMap<K, V>();
    }

    public static <K, V> ConcurrentMap<K, V> newConcurrentHashMap(int initialCapacity) {
        return new ConcurrentHashMap<K, V>(initialCapacity);
    }

    public static <K, V> ConcurrentMap<K, V> newConcurrentHashMap(Map<? extends K, ? extends V> map) {
        return new ConcurrentHashMap<K, V>(map);
    }

}
